@extends('layouts.vote.unregister.vote')
@section('titleRules')
    @include('app.vote.unregister.titleRules')
@endsection
@section('searchcure','cur')
@section('contents')
    <!-- 检索  S-->
    <div class="pb30">
        <div class="search mt20">
            <input type="text" class="searchInput">
            <span class="searchBtn">搜索</span>
        </div>
        <ul class="ui-list ui-list-link ui-link-search">

        </ul>
    </div>
@endsection
@section('script')
    <script src="/js/vote/unregister/list.js"></script>
    <script type="text/javascript">
		var voteList = '<li>' +
			'<div class="ui-list-img videoCon hasImg" style="background-image:url(\'?src?\');">' +
			'</div>' +
			'<div class="ui-list-info">' +
			'<h4 class="ui-nowrap">?title?</h4>' +
			'<p class="ui-nowrap-multi">' +
			'?content?</p>' +
			'<div class="state">' +
			'<span class="ranking">排名：?rank?</span>' +
			'<span class="votes">?votes?票</span>' +
			'</div>' +
			'</div>' +
			'</li>';
		$(function () {
			var voteItems;
			var scores;
			var item;

			function isEmpty(v) {
				return (
					(Array.isArray(v) && v.length == 0) || (Object.prototype.isPrototypeOf(v) && Object.keys(v).length == 0)
				);
			}

			function analyseSearchList() {
				if (!(isEmpty(voteItems) || isEmpty(item))) {
					var rank = 0;
					for (var index in voteItems) {
						rank++;
						if (voteItems[index]['name'] == item['name']) {
							var listTemp = '';
							listTemp = voteList.replace(/\?src\?/, item['cover']);
							listTemp = listTemp.replace(/\?title\?/, item['name']);
							listTemp = listTemp.replace(/\?content\?/, item['content']);
							listTemp = listTemp.replace(/\?rank\?/, rank.toString());
							listTemp = listTemp.replace(/\?votes\?/, scores[index]);
							$('ul.ui-link-search').append(listTemp);
						}
					}
				}
			}

			$('.searchBtn').on('click', function () {
				voteItems = new Array();
				item = new Array();
				$('ul.ui-link-search').empty();
				$.ajax({
					url: '{{env('LUA_SERVER')}}/api/vote/rank',
					data: 'vote_id={{$vote_id}}',
					type: 'get',
					dataType: 'json',
					success: function (response) {
						if (response.code == 200) {
							voteItems = response.items;
							scores = response.scores;
							analyseSearchList();
						}
					},
					error: function () {

					}
				});
				var $keyword = $(this).prev('input').val();
				$.ajax({
					url: '{{env('LUA_SERVER')}}/api/vote/search',
					data: 'vote_id={{$vote_id}}&q=' + $keyword,
					type: 'get',
					dataType: 'json',
					success: function (response) {
						if (response.code == 10000) {
							item = response.data;
							analyseSearchList();
						}
					},
					error: function () {

					}
				});
			})
		});
    </script>
@endsection

